package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeSet;
import java.util.prefs.BackingStoreException;

import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

import classifier.Classifier;
import classifier.DecisionTree;
import classifier.Feature;
import classifier.FeatureType;
import classifier.Item;

/**
 * 
 * @author Stephan E.G. Veenstra
 *
 */

public class ClassifierGUI extends JFrame implements ActionListener{

	JLabel label = new JLabel("Selecteer uw features:");
	
	JCheckBox abs = new JCheckBox("ABS");
	JCheckBox ac = new JCheckBox("AC");
	JCheckBox elecRamen = new JCheckBox("Electrische Ramen");
	JCheckBox elecSpiegels = new JCheckBox("Electrische Spiegels");
	JCheckBox turbo = new JCheckBox("Turbo");
	JCheckBox stuurBekr = new JCheckBox("Stuurbekrachtiging");
	JCheckBox spoiler = new JCheckBox("Spoiler");
	JCheckBox leer = new JCheckBox("Lederen Bekleding");
	
	JMenuBar menuBar = new JMenuBar();
	JMenu menu = new JMenu("opties");
	JMenuItem showGraph = new JMenuItem("toon boom");
	
	JTextField categorie = new JTextField("Check boxes to view...");
	
	TreeView treeView;
	
	Classifier classifier = new DecisionTree("src/TrainingSet.txt");
	
	public ClassifierGUI()
	{
		setLayout(new GridLayout(0,1));
		
		label.setFont(new Font("Arial",Font.BOLD,16));
		
		abs.addActionListener(this);
		ac.addActionListener(this);
		elecRamen.addActionListener(this);
		elecSpiegels.addActionListener(this);
		stuurBekr.addActionListener(this);
		spoiler.addActionListener(this);
		leer.addActionListener(this);
		categorie.addActionListener(this);
		
		add(label);
		
		add(abs);
		add(ac);
		add(elecRamen);
		add(elecSpiegels);
		add(turbo);
		add(stuurBekr);
		add(spoiler);
		add(leer);
		add(categorie);
		categorie.setEditable(false);
		categorie.setFont(new Font("Arial",Font.BOLD,14));
		
		setPreferredSize(new Dimension(200,400));
		setTitle("The Classifier v1.0");
		
		setJMenuBar(menuBar);
		menuBar.add(menu);
		menu.add(showGraph);
		showGraph.addActionListener(this);
		
		pack();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == showGraph)
		{
			if(treeView == null)
				treeView = new TreeView();
			else
				treeView.setVisible(!treeView.isVisible());
		}
		else
		{

			FeatureType yn = new FeatureType("YesNo"
					,new String[]{"yes","no"});
	
			Feature[] features = new Feature[]
			{ 	new Feature("Feature0",abs.isSelected() ? "yes" : "no",yn),
				new Feature("Feature1",ac.isSelected() ? "yes" : "no",yn),
				new Feature("Feature2",elecRamen.isSelected() ? "yes" : "no",yn),
				new Feature("Feature3",elecSpiegels.isSelected() ? "yes" : "no",yn),
				new Feature("Feature4",turbo.isSelected() ? "yes" : "no",yn),
				new Feature("Feature5",stuurBekr.isSelected() ? "yes" : "no",yn),
				new Feature("Feature6",spoiler.isSelected() ? "yes" : "no",yn),
				new Feature("Feature7",leer.isSelected() ? "yes" : "no",yn)
			};
			
			Item item = new Item("car",features);
	
			String category = classifier.assignCategory(item);
			
			categorie.setText("Categorie: " + classifier.assignCategory(item));
		}
	}
	
	public static void main(String[] args)
	{
		new ClassifierGUI();
	}

}
